package cn.iocoder.yudao.module.pay.wallet.dal.mysql;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.pay.wallet.dal.dataobject.PayWalletTransactionDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface PayWalletTransactionMapper extends CoreMapper<PayWalletTransactionDO> {

    default PageResult<PayWalletTransactionDO> selectPage(Long walletId, boolean income,
                                                          PageParam pageParam, LocalDateTime[] createTime) {
        CoreLambdaQueryWrapper<PayWalletTransactionDO> queryWrapper = new CoreLambdaQueryWrapper<>();
        queryWrapper.eqIfPresent(PayWalletTransactionDO::getWalletId, walletId);
        queryWrapper.gt(income, PayWalletTransactionDO::getPrice, 0); // 收入
        queryWrapper.lt(!income, PayWalletTransactionDO::getPrice, 0); // 支出
        queryWrapper.betweenIfPresent(PayWalletTransactionDO::getCreateTime, createTime);
        queryWrapper.orderByDesc(PayWalletTransactionDO::getId);
        return selectPage(pageParam, queryWrapper);
    }

    default PageResult<PayWalletTransactionDO> selectPage(Long walletId, Integer bizType,
                                                          PageParam pageParam, LocalDateTime[] createTime) {
        CoreLambdaQueryWrapper<PayWalletTransactionDO> queryWrapper = new CoreLambdaQueryWrapper<>();
        queryWrapper.eqIfPresent(PayWalletTransactionDO::getWalletId, walletId);
        queryWrapper.eqIfPresent(PayWalletTransactionDO::getBizType, bizType);
        queryWrapper.betweenIfPresent(PayWalletTransactionDO::getCreateTime, createTime);
        queryWrapper.orderByDesc(PayWalletTransactionDO::getId);
        return selectPage(pageParam, queryWrapper);
    }

    default Integer selectPriceSum(Long walletId, boolean income, LocalDateTime[] createTime) {
        // SQL sum 查询
        List<Map<String, Object>> result = selectMaps(new CoreQueryWrapper<PayWalletTransactionDO>()
                .select("SUM(price) AS priceSum")
                .gt(income, "price", 0) // 收入
                .lt(!income, "price", 0) // 支出
                .eq("wallet_id", walletId)
                .between("create_time", createTime[0], createTime[1]));
        // 获得 sum 结果
        Map<String, Object> first = CollUtil.getFirst(result);
        return MapUtil.getInt(first, "priceSum", 0);
    }

    default PayWalletTransactionDO selectByNo(String no) {
        return selectOne(PayWalletTransactionDO::getNo, no);
    }

    default PayWalletTransactionDO selectByBiz(String bizId, Integer bizType) {
        return selectOne(PayWalletTransactionDO::getBizId, bizId,
                PayWalletTransactionDO::getBizType, bizType);
    }

}




